package com.l024.loanthief.customView;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import androidx.annotation.Nullable;

import com.l024.loanthief.R;
import com.l024.loanthief.utils.DensityUtil;
import com.l024.loanthief.utils.Logutils;

/**
 * @author wu_ming_zhi_bei
 * @date 2021/1/14 13:13
 * @Notes 跳动的球
 */
public class BeatBallView extends View {
    private int ballColor;//球的颜色
    private int ballR;//球的半径
    private int ballSpan;//球之间的间隙
    private int ballNum;//球的个数
    private int beatHeight;//球跳动的高度
    private int beatTime;//跳动的时长
    private Context mContext;
    private Paint mPaint;//画笔
    private int positionCenter = 0;//当前球位置居中
    private int positionTop = 0;//当前球位置上
    private int positionBottom = 0;//当前球位置下

    public BeatBallView(Context context) {
        this(context,null);
    }

    public BeatBallView(Context context, @Nullable AttributeSet attrs) {
        this(context,attrs,0);
    }

    public BeatBallView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    /**
     * 初始化
     * @param context
     */
    private void init(Context context,AttributeSet attrs) {
        this.mContext = context;
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BeatBallView);
        ballColor = a.getInteger(R.styleable.BeatBallView_ballColor, Color.rgb(255,0,0));
        ballR = a.getInteger(R.styleable.BeatBallView_ballR, DensityUtil.px2dip(mContext,20));
        ballSpan = a.getInteger(R.styleable.BeatBallView_ballSpan,DensityUtil.px2dip(mContext,10));
        ballNum = a.getInteger(R.styleable.BeatBallView_ballNum,5);
        beatHeight = a.getInteger(R.styleable.BeatBallView_beatHeight,DensityUtil.px2dip(mContext,40));
        beatTime = a.getInteger(R.styleable.BeatBallView_beatTime,3);
        a.recycle();

        //初始化画笔
        mPaint = new Paint();
        mPaint.setColor(ballColor);
        mPaint.setStrokeWidth(1);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);

        //当前小球高度
        positionTop = ballR;
        positionCenter = ballR+beatHeight;
        positionBottom = ballR+beatHeight*2;
       startTopAnimation();
       startCenterAnimation();
       startBottomAnimation();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //画球
        drawBeat(canvas);
    }


    private void drawBeat(Canvas canvas) {
        for(int i=1;i<=ballNum;i++){
            int x = 2*ballR*(i-1)+ballSpan*(i-1)+ballR;
            if(i%3==1){
                //居中
                canvas.drawCircle(x, positionCenter,ballR,mPaint);
            }else if(i%3==2){
                //上面
                canvas.drawCircle(x, positionTop,ballR,mPaint);
            }else{
                //下面
                canvas.drawCircle(x, positionBottom,ballR,mPaint);
            }

        }
    }

    /**
     * 测量此组件需要的高度宽度
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int measureWidth = getMeasuredWidth();
        int measureHeight = getMeasuredHeight();
        //宽
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        //高
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    //
        switch (widthMode) {
            case MeasureSpec.EXACTLY://相当于我们设置为match_parent或者为一个具体的值
                measureWidth = widthSize;
                measureHeight = heightSize;
                break;
            case MeasureSpec.AT_MOST://相当于我们设置为wrap_content
                //计算宽度
                if(ballNum>1){
                    measureWidth = ballR*2*ballNum+(ballNum-1)*ballSpan;
                }else{
                    measureWidth = ballR*2;
                }
                measureHeight = 2*(ballR+beatHeight);
                break;
            case MeasureSpec.UNSPECIFIED://无限制 例如RecyclerView
                break;
        }

        Logutils.i("measureWidth:"+measureWidth);
        Logutils.i("measureHeight:"+measureHeight);
        setMeasuredDimension(measureWidth,measureHeight);
    }

    public void startAnimation() {
        //  平移
        ObjectAnimator translationYDown = new ObjectAnimator().ofFloat(this,"translationY",0,2*ballR,0,-2*ballR,0);
        //ObjectAnimator translationXUp = new ObjectAnimator().ofFloat(this,"translationY",2*ballR,0);
        translationYDown.setRepeatCount(ObjectAnimator.INFINITE);//设置无限重复
        translationYDown.setRepeatMode(ObjectAnimator.RESTART);//设置重复模式
        translationYDown.setDuration(3000);
        translationYDown.start();
    }

    private ValueAnimator topAnimator;
    public void startTopAnimation(){
        topAnimator = ValueAnimator.ofInt(positionTop,positionTop+beatHeight,ballR+2*beatHeight,positionTop+beatHeight,positionTop);
        topAnimator.setDuration(3000);
        topAnimator.setRepeatCount(ValueAnimator.INFINITE);
        topAnimator.setRepeatMode(ValueAnimator.RESTART);
        topAnimator.setInterpolator(new LinearInterpolator());
        topAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                positionTop = (int)animation.getAnimatedValue();
                invalidate();
            }
        });
        topAnimator.start();
    }

    private ValueAnimator centerAnimator;
    public void startCenterAnimation(){
        centerAnimator = ValueAnimator.ofInt(positionCenter,ballR+2*beatHeight,positionCenter,ballR,positionCenter);
        centerAnimator.setDuration(3000);
        centerAnimator.setRepeatCount(ValueAnimator.INFINITE);
        centerAnimator.setRepeatMode(ValueAnimator.RESTART);
        centerAnimator.setInterpolator(new LinearInterpolator());
        centerAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                positionCenter = (int)animation.getAnimatedValue();
                invalidate();
            }
        });
        centerAnimator.start();
    }

    private ValueAnimator bottomAnimator;
    public void startBottomAnimation(){
        bottomAnimator = ValueAnimator.ofInt(positionBottom,ballR+beatHeight,ballR,ballR+beatHeight,positionBottom);
        bottomAnimator.setDuration(3000);
        bottomAnimator.setRepeatCount(ValueAnimator.INFINITE);
        bottomAnimator.setRepeatMode(ValueAnimator.RESTART);
        bottomAnimator.setInterpolator(new LinearInterpolator());
        bottomAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                positionBottom = (int)animation.getAnimatedValue();
                invalidate();
            }
        });
        bottomAnimator.start();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        //回收资源
        if(topAnimator!=null){
            topAnimator.end();
        }
        if(centerAnimator!=null){
            centerAnimator.end();
        }
        if(bottomAnimator!=null){
            bottomAnimator.end();
        }
    }
}
